package com.github.soiff.shadow.plugin.utils;

import java.text.DecimalFormat;

/**
 * Created by Soiff on 2016/05/20.
 *
 * @author : Soiff
 */
public class NumberUtils {
    public static final DecimalFormat DECIMAL_FMT_2 = new DecimalFormat("0.00");

    public NumberUtils() {
    }

    public static String formatPrice(Number price) {
        return price == null?"":DECIMAL_FMT_2.format(price.doubleValue() / 100.0D);
    }

    public static String divide(Long a, Long b, int scale) {
        if(null != a && null != b) {
            Double result = Double.valueOf((double)a.longValue() / (double)b.longValue());
            return String.format("%." + scale + "f", new Object[]{result});
        } else {
            return "";
        }
    }

    public static String divide(Integer a, Integer b, int scale) {
        return divide(a, b, scale, 1);
    }

    public static String divide(Integer a, Integer b, int scale, int factor) {
        if(null != a && null != b) {
            Double result = Double.valueOf((double)a.intValue() / (double)b.intValue() * (double)factor);
            return String.format("%." + scale + "f", new Object[]{result});
        } else {
            return "";
        }
    }
}
